// 教师
const router = require('express').Router()
const db = require('../db')

// 得到教师列表
/*
除了这一列之外的其他列
*/
router.get('/list', (req, res) => {
    if (req.query.user.role !== 'admin') {
        res.json({ code: 403, msg: 'no permission' })
        return;
    }
    db.query('select id, name, sex, specialty from teacher', [], result => {
        res.json({ code: 0, msg: 'success', data: result })
    })
})


// 得到一个班级的所有学生
router.get('/getStudentList', (req, res) => {
    if (req.query.classId && req.query.role !== 'student') {
        db.query('SELECT id, name, sex, classId FROM student WHERE classId = ?;', req.query.classId, result => {
            res.json(result)
        })
    }
})


// 批量插入教师数据
router.post('/add', (req, res) => {
    let { user } = req.query
    if (user.role == 'student') return;
    let sql = ''
    let values = req.body
    console.log('values', values)
    sql = 'INSERT INTO teacher (id, name, sex, password, specialty) VALUES ?'
    if (!values[0][0]) {
        db.query(`SELECT max(id) FROM teacher `, {}, result => {
            let max = result[0]['max(id)'] + 1;
            for (let i = 0; i < values.length; i++) {
                values[i][0] = ++max
            }
            db.query(sql, [values], result => {
                res.json({
                    code: 0,
                    msg: '添加学生成功'
                })
            })
        })
    } else {
        db.query(sql, [values], result => {
            res.json({
                code: 0,
                msg: '添加学生成功'
            })
        })
    }

})


module.exports = router